# coding: utf-8
# author: t_wolf

from framework import *

if not web.config.second_load:
    web.config.second_load = True

    # 判断导入CONFIG为框架
    URL_ROUTE = entrance_init(CONFIGS)

    logger.debug(
        "Is Multi Apps: [%s], listening URL_ROUTE: %s" % (
            web.config.apps.enabled, URL_ROUTE
        )
    )

    # 创建Application
    apps = web.application(add_base_optional(to_children_app(URL_ROUTE)), globals())

    if not web.config.apps.enabled:

        # 会话传送
        if web.config.apps.single.session_enabled and web.ctx.get('session') is None:

            # Update 会话配置
            SESSION.session_update(web.config.apps.single.session_options)
            # 会话变量初始化
            session = SESSION(apps).init()
            apps.add_processor(
                web.loadhook(lambda: SESSION.transfer(session))
            )

        # 初始化
        EasyWeb.reg_background_worker(
            "Single",
            scan_back_worker(CONFIGS.FRAME.APP_PATH)
        )

    # 框架主体前置处理器
    apps.add_processor(web.loadhook(
        frame_handler
    ))

    # 框架加载默认页面
    apps.notfound = not_found_4_easy_web

    # 框架进程
    EasyWeb.process_init()
    FRAME_DEFINE.MAIN_PROCESS and EasyWeb.generate_background_threading().start()
    EasyWeb.start_public_pool()

    # 定义UWSGI的Application
    application = apps.wsgifunc()

# 利用web.py自身WSGI启动Application
if __name__ == '__main__':
    apps.run()
